﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;
using System.Media;

namespace DoAnHuongRung
{
    public partial class frmHoaDon : Form
    {
        public frmHoaDon()
        {
            InitializeComponent();
        }

        private void frmDonDatHang_Load(object sender, EventArgs e)
        {
            LoadDanhSachMonAn();
            XuLyKhach_Cu_Moi();
        }

        private void LoadDanhSachMonAn()
        {
            dtgHDMonAn.AutoGenerateColumns = false;
            dtgHDMonAn.DataSource = MonAnBUS.SelectAll();
            for (int i = 0; i < dtgHDMonAn.RowCount; i++)
            {
                dtgHDMonAn.Rows[i].Cells[3].Value = "1";
            }
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keydata)
        {
            if (keydata == Keys.Enter)
            {
                if (txtTimKiem.Focused == true && !txtTimKiem.Text.Equals(""))
                {
                    dtgHoaDon.AutoGenerateColumns = false;
                    dtgHDMonAn.AutoGenerateColumns = false;
                    List<KhachHangDTO> dskh = KhachHangBUS.Select_by_Name(txtTimKiem.Text);
                    if (dskh.Count != 0)
                    {
                        dtgDanhSachKhachHang.AutoGenerateColumns = false;
                        dtgDanhSachKhachHang.DataSource = dskh;
                    }
                }
            }
            return false;
        }

        private void txtTimKiem_Click(object sender, EventArgs e)
        {
            if (txtTimKiem.Text.Equals("Điền thông tin tiềm kiếm khách hàng vào đây rồi nhấn Enter"))
            {
                txtTimKiem.Text = "";
                txtTimKiem.ForeColor = Color.Black;
            }
        }

        public double sum = 0;
        private void btnChonMonAn_Click(object sender, EventArgs e)
        {
            try
            {
                sum = 0;
                List<HDMonAnDTO> list = new List<HDMonAnDTO>();                
                for (int i = 0; i < dtgHDMonAn.RowCount; i++)
                {                    
                    if (dtgHDMonAn.Rows[i].Cells[4].Value != null)
                    {
                        if ((bool)dtgHDMonAn.Rows[i].Cells[4].Value == true)
                        {
                            HDMonAnDTO ma = new HDMonAnDTO();
                            ma.MaMonAn = int.Parse(dtgHDMonAn.Rows[i].Cells[0].Value.ToString());
                            ma.TenMonAn = dtgHDMonAn.Rows[i].Cells[1].Value.ToString();
                            ma.DonGia = dtgHDMonAn.Rows[i].Cells[2].Value.ToString();
                            ma.SoLuong = int.Parse(dtgHDMonAn.Rows[i].Cells[3].Value.ToString());
                            ma.ThanhTien = (double.Parse((ma.SoLuong * double.Parse(ma.DonGia)).ToString())).ToString();
                            sum = sum + double.Parse(ma.ThanhTien);
                            list.Add(ma);
                        }
                    }
                }
                dtgHoaDon.AutoGenerateColumns = false;
                dtgHoaDon.DataSource = null;
                dtgHoaDon.Rows.Clear();
                dtgHoaDon.DataSource = list;                
                lblthanhtien.Text = sum.ToString() + "  VNĐ";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            try
            {
                lblMaKh.Text = "";
                txtTenKH.Text = "";
                rdbNamKH.Checked = true;
                dtpNgaySinhKH.Text = DateTime.Now.ToString();
                txtDiaChiKH.Text = "";
                txtSDTKH.Text = "";
                dtgHDMonAn.DataSource = null;
                dtgHoaDon.DataSource = null;
                lblthanhtien.Text = "";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnLapHoaDon_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtTenKH.Text.Equals("") != true)
                {
                    if (rdbkhachhangmoi.Checked == true)
                    {
                        KhachHangDTO kh = new KhachHangDTO();
                        kh.TenKH = txtTenKH.Text;
                        if (rdbNamKH.Checked == true)
                            kh.GioiTinh = "Nam";
                        else
                            kh.GioiTinh = "Nữ";
                        kh.NgaySinh = dtpNgaySinhKH.Value.ToString("MM/dd/yyyy");
                        kh.DiaChi = txtDiaChiKH.Text;
                        kh.SoDienThoai = txtSDTKH.Text;
                        KhachHangBUS.Insert(kh);

                        List<KhachHangDTO> dskh = KhachHangBUS.Select_by_Name(kh.TenKH);
                        txtMaKH.Text = dskh[0].MaKH.ToString();
                    }
                    HoaDonKhachHangDTO hd = new HoaDonKhachHangDTO();
                    hd.MaKH = int.Parse(txtMaKH.Text);
                    hd.NgayLap = DateTime.Now.ToString();
                    if (lblthanhtien.Text.Equals("0 VNĐ") == false)
                    {
                        hd.TriGia = sum.ToString();
                    }
                    bool b = false;
                    if (sum != 0)
                    {
                        b = HoaDonMonAnBUS.Insert(hd);
                    }
                    else
                    {
                        MessageBox.Show("Chưa chọn món ăn");
                    }
                    
                    for (int i = 0; i < dtgHoaDon.Rows.Count; i++)
                    {
                        ChiTietHoaDonDTO ct = new ChiTietHoaDonDTO();
                        ct.MaHD = HoaDonMonAnBUS.Search_ID(int.Parse(txtMaKH.Text), dtpNgaySinhKH.Text.ToString(), hd.TriGia);
                        ct.MaMonAn = int.Parse(dtgHoaDon.Rows[i].Cells[0].Value.ToString());
                        ct.SoLuong = int.Parse(dtgHoaDon.Rows[i].Cells[3].Value.ToString());
                        //ct.ThanhTien = (ct.SoLuong * double.Parse(dtgHDMonAn.Rows[i].Cells[2].Value.ToString())).ToString();
                        ct.ThanhTien = dtgHoaDon.Rows[i].Cells[4].Value.ToString();
                        b = ChiTietHoaDonBUS.Insert(ct);
                    }
                    //ct.MaHD = HoaDonMonAnBUS.Search_ID(hd.MaKH, hd.NgayLap, hd.TriGia);
                    if (b == true)
                    {
                        SoundPlayer s = new SoundPlayer(Properties.Resources.Windows_Notify);
                        s.Play();
                        MessageBox.Show("Đã lập hóa đơn thành công !!!");
                    }
                }
                else
                {
                    MessageBox.Show("Chưa chọn khách hàng");
                }
            }
            catch (Exception ex)
            {
                SoundPlayer s = new SoundPlayer(Properties.Resources.Windows_Error);
                s.Play();
                MessageBox.Show(ex.Message);
            }
        }


        private void XuLyKhach_Cu_Moi()
        {
            if (rdbkhachhangcu.Checked == true)
            {
                gbThongTinKhachHang.Enabled = false;
            }
            else
            {
                gbThongTinKhachHang.Enabled = true;
                //dtgDanhSachKhachHang.Enabled = false;
                txtMaKH.Text = "";
                txtTenKH.Text = "";
                rdbNamKH.Checked = true;
                dtpNgaySinhKH.Value = DateTime.Now;
                txtDiaChiKH.Text = "";
                txtSDTKH.Text = "";
                dtgDanhSachKhachHang.DataSource = null;
                dtgDanhSachKhachHang.Refresh();
            }
        }
        private void rdbkhachhangmoi_CheckedChanged(object sender, EventArgs e)
        {
            XuLyKhach_Cu_Moi();
        }

        private void dtgDanhSachKhachHang_SelectionChanged(object sender, EventArgs e)
        {
            try
            {
                txtMaKH.Text = dtgDanhSachKhachHang.CurrentRow.Cells[0].Value.ToString();
                txtTenKH.Text = dtgDanhSachKhachHang.CurrentRow.Cells[1].Value.ToString();
                if (dtgDanhSachKhachHang.CurrentRow.Cells[2].Value.ToString().Equals("Nam"))
                    rdbNamKH.Checked = true;
                else
                    rdbNuKH.Checked = false;
                dtpNgaySinhKH.Value = DateTime.Parse(dtgDanhSachKhachHang.CurrentRow.Cells[3].Value.ToString());
                txtDiaChiKH.Text = dtgDanhSachKhachHang.CurrentRow.Cells[4].Value.ToString();
                txtSDTKH.Text = dtgDanhSachKhachHang.CurrentRow.Cells[5].Value.ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Chưa có thông tin khách hàng");
            }
        }



        
    }
}
